#include "inputaddressdialog.h"
#include "ui_inputaddressdialog.h"
#include "iapmergedialog.h"

#include <QMessageBox>
#include <QPushButton>
#include <QLabel>

InputAddressDialog::InputAddressDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::InputAddressDialog)
{
    ui->setupUi(this);

    setWindowTitle(QApplication::applicationName());
    setWindowFlag(Qt::WindowContextHelpButtonHint, false);  // 去除标题栏的?
    setModal(true);

    QRegExp reg("[A-Fa-f0-9]{1,8}");
    ui->lineEdit->setValidator(new QRegExpValidator(reg, this));

   ui->buttonBox->button(QDialogButtonBox::Ok)->setText("确定");
   ui->buttonBox->button(QDialogButtonBox::Cancel)->setText("取消");

   QObject::connect(ui->labelBaseAddress,
                    static_cast<void (QLabel::*)(const QString&)>(&QLabel::linkActivated),
                    ui->lineEdit,
                    static_cast<void (QLineEdit::*)(const QString&)>(&QLineEdit::setText));
}

InputAddressDialog::~InputAddressDialog()
{
    delete ui;
}

void InputAddressDialog::accept()
{
    QString text = ui->lineEdit->text();
    uint32_t addr = text.toInt(NULL, 16);
    if(addr < FLASH_ADDRESS_BEGIN || addr > FLASH_ADDRESS_END){
        QMessageBox::critical(this, "友情提示", "输入错误，地址范围不合法！");
        return;
    }
    done(QDialog::Accepted);
}

void InputAddressDialog::reject()
{
    done(QDialog::Rejected);
}

uint32_t InputAddressDialog::getAddress()
{
    return ui->lineEdit->text().toInt(NULL, 16);
}
